package com.example.syyao.myapp;


import android.annotation.SuppressLint;
import android.content.*;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.syyao.myapp.Adapter.MyAdapt;
import com.example.syyao.myapp.Personal.PersonalFragment;
import com.example.syyao.myapp.Sign.Sign_in;
import com.example.syyao.myapp.Sign.WelcomeActivity;
import com.example.syyao.myapp.Tool.PermisionUtils;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity  implements ViewPager.OnPageChangeListener{

    public  static final String PREFERENCE_NAME="MyImage";

    public static final int MODE=MODE_PRIVATE;


    private FindFragment findFragment;
    private MainFragment mainFragment;
    private com.example.syyao.myapp.Personal.PersonalFragment PersonalFragment;
    private ArrayList<Fragment> fragmentList;
    //包裹点点的LinearLayout
    private ViewGroup group;

    private IntentFilter intentFilter;

    private MyAdapt myAdapt;
    private ViewPager viewPager;
    private FragmentSkipInterface mFragmentSkipInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //状态栏半透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //状态栏全透明（不会修改状态栏系统图标问题，白底的情况会导致看不清图标）
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }*/

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取传递的值

        SharedPreferences sharedPreferences=getSharedPreferences(PREFERENCE_NAME,MODE);
        boolean isFirst=sharedPreferences.getBoolean("isFirst",true);
        if(isFirst){
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, WelcomeActivity.class);
            startActivity(intent);
        }

//        boolean isLogin=sharedPreferences.getBoolean("isLogin",false);
//        if(!isLogin){
//            Intent intent = new Intent();
//            intent.setClass(MainActivity.this, Sign_in.class);
//            startActivity(intent);
//        }

        viewPager = findViewById(R.id.viewPager);
        group = (LinearLayout)findViewById(R.id.viewGroup);

        //有多少张图就有多少个点点

        super.onStart();
        initfragment();
        initgroup();
        myAdapt = new MyAdapt(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(myAdapt);
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(1);
        PermisionUtils.verifyStoragePermissions(this);
        intentFilter = new IntentFilter();
        intentFilter.addAction("action.refreshActivity_A");
        registerReceiver(mRefreshBroadcastReceiver, intentFilter);
    }


    @Override
    public void onRestart() {
        super.onRestart();


    }

    public void initgroup(){
        int page = fragmentList.size();
        for (int i = 0; i < page; i++) {
            Point point = new Point(this);
//            System.out.println(i+"  "+state);
            if (i == 0) {
                point.setSelected(true);
            } else {
                point.setSelected(false);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
            params.leftMargin = 0;
            params.topMargin = 0;
            group.addView(point, params);
        }
    }

    public void initfragment(){
        findFragment = new FindFragment();
        mainFragment = new MainFragment();
        PersonalFragment = new PersonalFragment();

        fragmentList = new ArrayList<>();
        fragmentList.add(findFragment);
        fragmentList.add(mainFragment);
        fragmentList.add(PersonalFragment);
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        group.removeAllViews();
        //总共的页数
        int page = fragmentList.size();
        for (int i = 0; i < page; i++) {
            Point point = new Point(this);
//            System.out.println(i+"  "+state);
            if (i == position) {
                point.setSelected(true);
            } else {
                point.setSelected(false);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
            params.leftMargin = 0;
            params.topMargin = 0;
            group.addView(point, params);
        }

        int nowPage=viewPager.getCurrentItem();
        if(nowPage==0|| nowPage==2){
            removePoint();
        }
        if(position==2){
            SharedPreferences sharedPreferences=getSharedPreferences(PREFERENCE_NAME,MODE);
            boolean isLogin = sharedPreferences.getBoolean("isLogin", false);
            if (!isLogin) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, Sign_in.class);
                startActivity(intent);
            }
        }

    }

    private void removePoint() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                    handler.sendEmptyMessage(0x123);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            group.removeAllViews();
        }
    };

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void setFragmentSkipInterface(FragmentSkipInterface fragmentSkipInterface) {
        mFragmentSkipInterface = fragmentSkipInterface;
    }

    /** Fragment跳转 */
    public void skipToFragment(){
        if(mFragmentSkipInterface != null){
            mFragmentSkipInterface.gotoFragment(viewPager);
        }
    }

    public interface FragmentSkipInterface {
        /** ViewPager中子Fragment之间跳转的实现方法
         * @param viewPager*/
        void gotoFragment(ViewPager viewPager);
    }


    private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("action.refreshActivity_A"))
            {
                //refresh();
                viewPager.setCurrentItem(1);
            }
        }
    };



    protected void refresh() {


        onCreate(null);

    }
    @Override
    public void onDestroy() {


        unregisterReceiver(mRefreshBroadcastReceiver);
        super.onDestroy();
    }


}
